package com.whfc.wxmp.api.mach.v1.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.entity.dto.WxDeviceBindCheckDTO;
import com.whfc.entity.param.WxBindCheckParam;
import com.whfc.fuum.entity.WxUser;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.param.*;
import com.whfc.wxmp.api.mach.v1.service.WxmpMachService;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author sun_guodong
 * @description
 * @date 2020-11-17
 */
@RestController
@RequestMapping("/mp/api/v1/mach")
public class WxmpMachController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxmpMachService wxMachService;

    @GetMapping("/info")
    public Result machInfo(@CurrentWxUser WxUser wxUser,
                           @RequestParam(value = "machId") Integer machId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "查询设备信息", RequestConetxtUtil.getParameters());
        AppMachDTO machDTO = wxMachService.getMachInfo(machId);
        return ResultUtil.success(machDTO);
    }

    @PostMapping("/add")
    public Result add(@CurrentWxUser WxUser wxUser,
                      @Validated @RequestBody WxMachAddParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "添加并绑定设备", RequestConetxtUtil.getParameters());
        wxMachService.add(wxUser, param);
        return ResultUtil.success();
    }

    @PostMapping("/edit")
    public Result edit(@CurrentWxUser WxUser wxUser,
                       @Validated @RequestBody MachEditParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                wxUser.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "编辑设备", RequestConetxtUtil.getParameters());
        wxMachService.edit(param);
        return ResultUtil.success();
    }


    @PostMapping("/del/{machId}")
    public Result delMach(@CurrentWxUser WxUser user, @PathVariable("machId") Integer machId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "小程序删除设备", RequestConetxtUtil.getParameters());
        wxMachService.delMach(machId);
        return ResultUtil.success();
    }


    @PostMapping("/bind/device")
    public Result bindDevice(@CurrentWxUser WxUser user, @Validated @RequestBody DeviceMachBindParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "已有设备绑定硬件", RequestConetxtUtil.getParameters());
        wxMachService.bindDevice(user, request);
        return ResultUtil.success();
    }


    @PostMapping("/unbind/device/{machId}")
    public Result unbindDevice(@CurrentWxUser WxUser user, @PathVariable("machId") Integer machId) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "设备解绑硬件", RequestConetxtUtil.getParameters());
        wxMachService.unbindDevice(user, machId);
        return ResultUtil.success();
    }

    /**
     * 绑定校验
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/bindCheck")
    public Result bindCheck(@CurrentWxUser WxUser user, @RequestBody @Validated WxBindCheckParam request) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "硬件绑定前检验", RequestConetxtUtil.getParameters());
        String sn = request.getSn();
        WxDeviceBindCheckDTO result = wxMachService.bindCheck(sn);
        return ResultUtil.success(result);
    }

    @PostMapping("/setOilAdd")
    public Result setOilAdd(@CurrentWxUser WxUser user,
                            @Validated @RequestBody MachSetOilAddParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "设备标定加油量", RequestConetxtUtil.getParameters());
        wxMachService.setOilAdd(param);
        return ResultUtil.success();
    }

    @GetMapping("/getOilAdd")
    public Result getOilAdd(@CurrentWxUser WxUser user,
                            @RequestParam("machId") Integer machId,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "获取标定加油量详情", RequestConetxtUtil.getParameters());
        ListData<MachOilAdd> listData = wxMachService.getOilAdd(machId, date);
        return ResultUtil.success(listData);
    }

}
